package com.yangzheandroid.rxmvp.base;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.yangzheandroid.retrofitutils.utils.AppManager;
import com.yangzheandroid.rxmvp.APP;
import com.yangzheandroid.rxmvp.R;
import com.yangzheandroid.rxmvp.di.component.ActivityComponent;
import com.yangzheandroid.rxmvp.di.component.DaggerActivityComponent;
import com.yangzheandroid.rxmvp.di.module.ActivityModule;
import com.yangzheandroid.rxmvp.widget.dialog.PrettyProgressDialog;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Author：Jalen on 2016/7/17 10:16
 * Contact: studylifetime@sina.com
 */
public abstract class BaseActivity<T extends BasePresenter> extends SimpleActivity implements BaseView {
    @Inject
    protected T mPresenter;

    public PrettyProgressDialog mPrettyProgressDialog;

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        initInject();
        if (mPresenter != null)
            mPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null)
            mPresenter.detachView();
        super.onDestroy();
    }

    protected abstract void initInject();

    protected ActivityComponent getActivityComponent(){
        return  DaggerActivityComponent.builder()
                .appComponent(APP.getAppComponent())
                .activityModule(getActivityModule())
                .build();
    }

    protected ActivityModule getActivityModule(){
        return new ActivityModule(this);
    }

    //返回键返回事件->针对fragment的操作
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
                finish();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * 展示加载框
     */
    public PrettyProgressDialog showLoadingDialog() {
        mPrettyProgressDialog = new PrettyProgressDialog(this);
        mPrettyProgressDialog.setText(R.string.dialong_loading_tips);
        mPrettyProgressDialog.show();
        return mPrettyProgressDialog;
    }

    /**
     * 停止对话框
     */
    public void stopLoadingDialog() {
        if (null != mPrettyProgressDialog && mPrettyProgressDialog.isShowing()) {
            mPrettyProgressDialog.dismiss();
            mPrettyProgressDialog = null;
        }
    }
}
